package com.example.myapplication.ArrayAdapterInclude;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.myapplication.ContentTest.DictionaryAddActivity;
import com.example.myapplication.ContentTest.DictionaryInfo;
import com.example.myapplication.ContentTest.DictionaryInfoActivity;
import com.example.myapplication.ContentTest.VocabularyProvider;
import com.example.myapplication.R;
import java.util.ArrayList;

public class DictionaryArrayListAdapter extends ArrayAdapter<DictionaryInfo> {
  private ArrayList<DictionaryInfo> listData;
  private int convertViewId = -1;
  DictionaryInfoActivity dictionaryInfoActivity;

  public DictionaryArrayListAdapter(Context context, DictionaryInfoActivity activity,ArrayList<DictionaryInfo> data) {
    super(context, R.layout.single_dictionary_info_layout, data);
    listData=data;
    dictionaryInfoActivity=activity;
    convertViewId=R.layout.single_dictionary_info_layout;
  }

  @Override public int getCount() {
    return listData.size();
  }

  @Nullable @Override public DictionaryInfo getItem(int position) {
    return listData.get(position);
  }

  @NonNull @Override
  public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    if (convertView == null) {
      convertView = LayoutInflater.from(getContext()).inflate(convertViewId, parent, false);
    }
    SetButton(convertView,position);
    String str=ConvertText(getItem(position));
    TextView textView = convertView.findViewById(R.id.dict_info);
    textView.setText(str);
    return convertView;
  }

  private void SetButton(View convertView,int position) {
    ImageButton operationButton = convertView.findViewById(R.id.operate_button);
    operationButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        ShowPopupMenu(operationButton,position);
      }
    });
  }

  private void ShowPopupMenu(ImageButton imageButton,int position) {
    PopupMenu popupMenu = new PopupMenu(getContext(), imageButton);
    popupMenu.getMenuInflater().inflate(R.menu.dictionary_popup_menu,popupMenu.getMenu());
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
      @Override public boolean onMenuItemClick(MenuItem item) {
        if (item.getItemId() == R.id.delete_data) {
          if (DeleteWord(listData.get(position).getWord())) {
            listData.remove(position);
            notifyDataSetChanged();
            System.out.println("listData的大小为"+ listData.size());
            dictionaryInfoActivity.UpdateOriginData();
          }

        } else if (item.getItemId() == R.id.modify_data) {
          JumpToUpdate(position);
        } else {
          return false;
        }
        return true;
      }

    });
    popupMenu.show();
  }

  private void JumpToUpdate(int position) {
    int RESULT_OK=1;
    DictionaryInfo dictionaryInfo = listData.get(position);
    //ActivityResultLauncher<Intent> launcher = dictionaryInfoActivity.registerForActivityResult(
    //    new ActivityResultContracts.StartActivityForResult(),result -> {
    //      if (result.getResultCode() == RESULT_OK) {
    //        //处理子Activity返回的结果
    //        dictionaryInfoActivity.UpdateOriginData();
    //        ;
    //      }});
    Intent intent = new Intent(getContext(), DictionaryAddActivity.class);
    intent.putExtra("word", dictionaryInfo.getWord());
    intent.putExtra("illustration", dictionaryInfo.getIllustration());
    getContext().startActivity(intent);

  }

  private boolean DeleteWord(String word) {
    int count = getContext().getContentResolver()
        .delete(VocabularyProvider.CONTENT_URI, VocabularyProvider.COLUMN_WORD + "=?",
            new String[] { word });
    if (count > 0) {

      Toast.makeText(getContext(), "已成功删除", Toast.LENGTH_SHORT).show();
      return true;
    } else {
      Toast.makeText(getContext(), "无法删除该单词", Toast.LENGTH_SHORT).show();
      return false;
    }
  }

  private String ConvertText(DictionaryInfo item) {
    String wordStr = item.getWord();
    String explainStr = item.getIllustration();
    String convertStr = getContext().getString(R.string.dictionary_info, wordStr, explainStr);
    return convertStr;
  }

  public void setListData(
      ArrayList<DictionaryInfo> listData) {
    //this.listData.clear();
    this.listData = listData;

    notifyDataSetChanged();
  }
}
